class Solution {
public:
    TreeNode* travesal(vector<int>& inorder, vector<int>& postorder){
      if(postorder.size() == 0) return NULL;
      int rootValue = postorder[postorder.size() - 1];
      TreeNode* root = new TreeNode(rootValue);

      if(postorder.size() == 1) return root;

      int index = 0;
      for(index = 0;index < inorder.size();index++){
        if(inorder[index] == rootValue){
          break;
        }
      }

      vector<int> leftInoder(inorder.begin(),inorder.begin()+index);
      vector<int> rightInoder(inorder.begin()+index+1,inorder.end());
      
      postorder.resize(postorder.size() - 1);
      vector<int> leftPostorder(postorder.begin(),postorder.begin()+leftInoder.size());
      vector<int> rightPostorder(postorder.begin()+leftInoder.size(),postorder.end());

      root->left = travesal(leftInoder,leftPostorder);
      root->right = travesal(rightInoder,rightPostorder);

      return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
      if(postorder.size() == 0||postorder.size() == 0) return NULL;
      return travesal(inorder,postorder);
    }
};
